#pragma once
#include <iostream>
#include <ctime>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
namespace bitlog{
    namespace util{
        class Date{
            public:
                static size_t now() {
                    return (size_t)time(nullptr);
                }
                };
                class File {
                    public:
                        static bool exists(const std::string& pathname){
                            struct stat st;
                            if(stat(pathname.c_str(),&st)<0){
                                return false;
                                //stat是一个有跨操作系统支持的
                            }
                            else
                                return true;
                            //return (access(pathname.c_str(),F_OK) == 0);
                            //access系统调用通过传输路径，和F_OK为判断文件是否存在选项来判断文件是否存在，存在返回0，不存在返回-1
                        }
                        static std::string path(const std::string& pathname){
                            size_t pos = pathname.find_last_of("/\\");
                            if(pos == std::string::npos)return ".";
                            return pathname.substr(0,pos+1);
                        }
                        static void createDirectory(const std::string& pathname){
                            size_t pos = 0,idx = 0;
                            while(idx<pathname.size()){
                            pos = pathname.find_first_of("/\\",idx);
                            if(pos == std::string::npos){
                                mkdir(pathname.c_str(),0777);
                              
                            }
                            std::string parent_dir = pathname.substr(0,pos+1);
                            if (exists(parent_dir) == true) {
                                idx = pos+1;
                                continue;
                            }
                                mkdir(parent_dir.c_str(),0777);
                                idx = pos+1;
                            }
                        }
                };
        
    }
}